package chess;

import java.util.Collections;
import java.util.Arrays;

public class ChessGenerator {
	
	public static Piece[] generatePieces() {
		Piece[] pieces = new Piece[32];
		
		Integer[] x = { 1, 2, 3, 4, 5, 6, 7, 8 };
		Integer[] y = { 1, 2, 3, 4, 5, 6, 7, 8 };
		
		Collections.shuffle(Arrays.asList(x));
		Collections.shuffle(Arrays.asList(y));	
		
		boolean white = true;
		String type = "pawn";
		for (int i = 0; i < 32; i++) {
			// handle color
			if (i > 15 & white) {
				white = false;
			}
			
			// handle array shift for unique int pairs
			if (i % 8 == 0) {
				int temp = x[0];
				for (int j = 0; j < x.length - 1; j++) {
					x[j] = x[j + 1];
				}
				x[x.length - 1] = temp;
				//System.out.println("x arrayshift");
			}
			
			// handle piece type
			int j = i % 16;
			if (j < 8 & !type.equals("pawn")) {
				type = "pawn";
			} else if (j >= 8 && j < 10 & !type.equals("rook")) {
				type = "rook";
			} else if (j >= 10 && j < 12 & !type.equals("knight")) {
				type = "knight";
			} else if (j >= 12 && j < 14 & !type.equals("bishop")) {
				type = "bishop";
			} else if (j >= 14 && j < 15 & !type.equals("queen")) {
				type = "queen";
			} else if (j >= 15 && j < 16 & !type.equals("king")) {
				type = "king";
			}
			
			// add piece
			pieces[i] = new Piece(x[i % 8].intValue(), y[i % 8].intValue(), white, type);
			// check generated pieces
			System.out.println(pieces[i].stringRepresentation());
		}
		
		return pieces;
	}
}